﻿var myApp = angular.module('Todos', ['firebase', 'ui.bootstrap']);

myApp.controller('HomeController', ['$scope', '$firebaseArray', function ($scope, $firebaseArray) {
    // add instance of FBArray
    var ref = new Firebase('https://315demo.firebaseio.com/tonotdo');

    // 
    $scope.greeting = "oi mundo";

    // Firbase will not accept undefined values, so set defualt vals...
    $scope.inputCompleted = false;
    $scope.inputTask = "";

    // contain all data / array
    // create a synchronized array
    $scope.todos = $firebaseArray(ref);

    // task - title of todo
    // isCompleted - if task has been completed
    function Todo(task, isCompleted) {
        this.task = task,
        this.isCompleted = isCompleted
    }

    // CREATE - add new todos
    $scope.addTask = function () {
        // grab content from fields, make new object, add to array
        console.log("input... ", $scope.inputTask, $scope.inputCompleted)
        var nTodo = new Todo($scope.inputTask, $scope.inputCompleted);
        $scope.todos.$add(nTodo); // saves data to FB
    };


}]);